package com.hex.ds.hdtp.core.inf.script.transfer.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.common.page.converter.IPageAndMyBatisPlusPageConverter;
import com.hex.ds.hdtp.core.inf.script.transfer.mapper.IScriptConvertTargetListMapper;
import com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertTargetListPo;
import com.hex.ds.hdtp.core.inf.script.transfer.repository.IScriptConvertTargetListRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.inf.script.transfer.repository.impl
 * @ClassName ScriptConvertTargetListRepository
 * @Description: 脚本转换-转换后-数据服务接口实现
 * @Author: wz.li
 * @Date 2023/8/14 16:38
 * @Version v2.0.2
 **/
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptConvertTargetListRepository implements IScriptConvertTargetListRepository {

    private final IScriptConvertTargetListMapper scriptConvertTargetListMapper;

    private final IPageAndMyBatisPlusPageConverter pageConverter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [scriptConvertTargetListPo, page] <br>
     * @Return: com.hex.ds.hdtp.core.inf.common.page.Page<com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertTargetListPo> <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/16 13:44 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Page<ScriptConvertTargetListPo> queryListByPage(ScriptConvertTargetListPo scriptConvertTargetListPo, Page page) {
        final IPage tmpPageResult = scriptConvertTargetListMapper.queryListByPage(
                pageConverter.toMyBatisPlusPage(page)
                , scriptConvertTargetListPo
        );
        final List list = tmpPageResult.getRecords();
        final Page pageResult = pageConverter.toPage(tmpPageResult);
        return pageResult.setData(list);
    }

    /*
     * @Method: queryListByTaskId <br>
     * @Param: [convertTaskId] <br>
     * @Return: java.util.List<com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertTargetListPo> <br>
     * @Description：根据指定转换任务主键查询对应的转换文件清单<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/14 20:29 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public List<ScriptConvertTargetListPo> queryListByTaskId(String convertTaskId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("convert_task_pk_id", convertTaskId);
        return scriptConvertTargetListMapper.selectList(queryWrapper);
    }

    /*
     * @Method: queryById <br>
     * @Param: [pkId] <br>
     * @Return: com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertTargetListPo <br>
     * @Description：根据主键查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/15 10:27 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public ScriptConvertTargetListPo queryById(String pkId) {
        return scriptConvertTargetListMapper.selectById(pkId);
    }

    /*
     * @Method: queryBySourceListIdAndConvertFileName <br>
     * @Param: [convertSourceListId, convertFileName] <br>
     * @Return: com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertTargetListPo <br>
     * @Description：根据转换原脚本清单主键和转换后脚本名称查询对应的转换清单记录<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/16 16:52 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public ScriptConvertTargetListPo queryBySourceListIdAndConvertFileName(String convertSourceListId, String convertFileName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("convert_source_list_pk_id", convertSourceListId);
        queryWrapper.eq("convert_file_name", convertFileName);
        return scriptConvertTargetListMapper.selectOne(queryWrapper);
    }


    /*
     * @Method: add <br>
     * @Param: [scriptConvertTargetListPo] <br>
     * @Return: int <br>
     * @Description：新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/14 16:38 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public int add(ScriptConvertTargetListPo scriptConvertTargetListPo) {
        return scriptConvertTargetListMapper.insert(scriptConvertTargetListPo);
    }

    /*
     * @Method: modifyById <br>
     * @Param: [scriptConvertTargetListPo] <br>
     * @Return: int <br>
     * @Description：根据主键更新<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/15 9:26 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public int modifyById(ScriptConvertTargetListPo scriptConvertTargetListPo) {
        return scriptConvertTargetListMapper.updateById(scriptConvertTargetListPo);
    }

    /*
     * @Method: removeByTaskId <br>
     * @Param: [convertTaskPkId] <br>
     * @Return: int <br>
     * @Description：根据转换任务主键删除对应的转换文件记录<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/16 13:45 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public int removeByTaskId(String convertTaskPkId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("convert_task_pk_id", convertTaskPkId);
        return scriptConvertTargetListMapper.delete(queryWrapper);
    }

    /*
     * @Method querySuccessListByPage <br>
     * @param scriptConvertSourceListPo
     * @param toPage <br>
     * @Return com.hex.ds.hdtp.core.inf.common.page.Page<com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertSourceListPo> <br>
     * @Description 查询最新成功的脚本<br>
     * @Author Yin.Yang<br>
     * @Date 2023/8/30 10:30 <br>
     * @Version V1.0<br>
     */
    @Override
    public Page<ScriptConvertTargetListPo> querySuccessListByPage(ScriptConvertTargetListPo scriptConvertTargetListPo, Page toPage) {
        final IPage tmpPageReslt = scriptConvertTargetListMapper.querySuccessListByPage(
                pageConverter.toMyBatisPlusPage(toPage),
                scriptConvertTargetListPo
        );
        final List list = tmpPageReslt.getRecords();
        final Page pageResult = pageConverter.toPage(tmpPageReslt);
        return pageResult.setData(list);
    }
}
